#!/usr/bin/env python3
"""
PyTorch GPU功能测试和性能基准测试主运行脚本
"""

import os
import sys
import subprocess
from datetime import datetime

def run_test(script_name, description):
    """运行单个测试脚本"""
    print(f"\n{'='*60}")
    print(f"运行: {description}")
    print(f"{'='*60}")
    
    try:
        # 激活conda环境并运行脚本
        cmd = f"conda activate pytorch-gpu-test && python {script_name}"
        result = subprocess.run(cmd, shell=True, executable="/bin/zsh", capture_output=True, text=True)
        
        if result.returncode == 0:
            print(f"✓ {description} 完成")
            print(result.stdout)
        else:
            print(f"✗ {description} 失败")
            print(f"错误输出: {result.stderr}")
            print(f"标准输出: {result.stdout}")
            
    except Exception as e:
        print(f"✗ {description} 执行异常: {e}")

def main():
    """主函数"""
    print("PyTorch GPU功能测试和性能基准测试")
    print("=" * 60)
    print(f"开始时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    
    # 检查conda环境
    print("\n检查conda环境...")
    try:
        result = subprocess.run(
            "conda activate pytorch-gpu-test && python -c \"import torch; print(f'PyTorch版本: {torch.__version__}')\"",
            shell=True, executable="/bin/zsh", capture_output=True, text=True
        )
        if result.returncode == 0:
            print("✓ Conda环境检查通过")
            print(result.stdout.strip())
        else:
            print("✗ Conda环境检查失败")
            print("请确保已创建并激活 'pytorch-gpu-test' 环境")
            return
    except Exception as e:
        print(f"✗ 环境检查异常: {e}")
        return
    
    # 运行各个测试
    tests = [
        ("src/gpu_detection.py", "GPU功能检测"),
        ("src/performance_test.py", "性能基准测试"),
        ("src/memory_test.py", "内存使用测试")
    ]
    
    for script, description in tests:
        run_test(script, description)
    
    print(f"\n{'='*60}")
    print("所有测试完成!")
    print(f"结束时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"测试结果保存在 'results/' 目录中")
    print("=" * 60)

if __name__ == "__main__":
    main()
